package com.ruoyi.user.controller.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SpffVideoInteractions;
import com.ruoyi.system.service.ISpffVideoInteractionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 视频互动Controller
 * 
 * @author xjj
 * @date 2025-09-11
 */
@RestController
@RequestMapping("/userApi/interactions")
public class USpffVideoInteractionsController extends BaseController
{
    @Autowired
    private ISpffVideoInteractionsService spffVideoInteractionsService;
    
    @Autowired
    private com.ruoyi.system.service.impl.SpffVideosServiceImpl spffVideosService;

    /**
     * 查询视频互动列表
     */
    @PreAuthorize("@ss.hasPermi('system:interactions:list')")
    @GetMapping("/list")
    public TableDataInfo list(SpffVideoInteractions spffVideoInteractions)
    {
        startPage();
        List<SpffVideoInteractions> list = spffVideoInteractionsService.selectSpffVideoInteractionsList(spffVideoInteractions);
        return getDataTable(list);
    }

    /**
     * 导出视频互动列表
     */
    @PreAuthorize("@ss.hasPermi('system:interactions:export')")
    @Log(title = "视频互动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpffVideoInteractions spffVideoInteractions)
    {
        List<SpffVideoInteractions> list = spffVideoInteractionsService.selectSpffVideoInteractionsList(spffVideoInteractions);
        ExcelUtil<SpffVideoInteractions> util = new ExcelUtil<SpffVideoInteractions>(SpffVideoInteractions.class);
        util.exportExcel(response, list, "视频互动数据");
    }

    /**
     * 获取视频互动详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:interactions:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(spffVideoInteractionsService.selectSpffVideoInteractionsById(id));
    }

    /**
     * 点赞/取消点赞视频（切换点赞状态）
     * 前端调用此接口进行点赞操作，返回操作后的状态
     */
    @PostMapping("/like/{videoId}")
    public AjaxResult toggleLike(@PathVariable("videoId") Long videoId)
    {
        try {
            Long userId = com.ruoyi.common.utils.SecurityUtils.getUserId();
            
            // 调用视频服务的点赞切换方法
            int result = spffVideosService.toggleLike(videoId, userId);
            
            // 获取操作后的点赞状态
            boolean isLiked = result == 1;
            Map<String, Boolean> res=new HashMap<>();
            res.put("isLiked", isLiked);
            return success(res);
        } catch (Exception e) {
            return error("操作失败：" + e.getMessage());
        }
    }

    /**
     * 检查用户对视频的点赞状态
     * 前端可以用这个接口单独检查某个视频的点赞状态
     */
    @GetMapping("/like/status/{videoId}")
    public AjaxResult getLikeStatus(@PathVariable("videoId") Long videoId)
    {
        try {
            Long userId = com.ruoyi.common.utils.SecurityUtils.getUserId();
            boolean isLiked = spffVideosService.isUserLiked(videoId, userId);
            
            return success(isLiked);
        } catch (Exception e) {
            return error("获取点赞状态失败：" + e.getMessage());
        }
    }

    /**
     * 收藏/取消收藏视频（切换收藏状态）
     * 前端调用此接口进行收藏操作，返回操作后的状态
     */
    @PostMapping("/collect/{videoId}")
    public AjaxResult toggleCollect(@PathVariable("videoId") Long videoId)
    {
        try {
            Long userId = com.ruoyi.common.utils.SecurityUtils.getUserId();
            
            // 调用视频服务的收藏切换方法
            int result = spffVideosService.toggleCollect(videoId, userId);
            
            // 获取操作后的收藏状态
            boolean isCollected = result == 1;
            
            // 直接返回isCollected状态，前端根据状态判断
            return success(isCollected);
            
        } catch (Exception e) {
            return error("操作失败：" + e.getMessage());
        }
    }

    /**
     * 检查用户对视频的收藏状态
     * 前端可以用这个接口单独检查某个视频的收藏状态
     */
    @GetMapping("/collect/status/{videoId}")
    public AjaxResult getCollectStatus(@PathVariable("videoId") Long videoId)
    {
        try {
            Long userId = com.ruoyi.common.utils.SecurityUtils.getUserId();
            boolean isCollected = spffVideosService.isUserCollected(videoId, userId);
            
            return success(isCollected);
        } catch (Exception e) {
            return error("获取收藏状态失败：" + e.getMessage());
        }
    }

    /**
     * 新增视频互动
     */
    @PreAuthorize("@ss.hasPermi('system:interactions:add')")
    @Log(title = "视频互动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SpffVideoInteractions spffVideoInteractions)
    {
        return toAjax(spffVideoInteractionsService.insertSpffVideoInteractions(spffVideoInteractions));
    }

    /**
     * 修改视频互动
     */
    @PreAuthorize("@ss.hasPermi('system:interactions:edit')")
    @Log(title = "视频互动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SpffVideoInteractions spffVideoInteractions)
    {
        return toAjax(spffVideoInteractionsService.updateSpffVideoInteractions(spffVideoInteractions));
    }

    /**
     * 删除视频互动
     */
    @PreAuthorize("@ss.hasPermi('system:interactions:remove')")
    @Log(title = "视频互动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(spffVideoInteractionsService.deleteSpffVideoInteractionsByIds(ids));
    }
}
